import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../utils/index'
import DB from '../../../app/db';
import nattyStorage from 'natty-storage';
import StorageUtils from '../../../app/storage'
import { Control } from 'react-keeper'

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    userInfo: {},
    doors: [],
    courtsAndDoors: []
  },

  customInit() {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    this.updateComponent();
    this.getDoorGroupCourt(StorageUtils.getStorageItem('proprietor').id);
  },

  // 是否已经报名
  initCast(proprietorId, code) {
    let t = this;
    let courtsAndDoors = t.data.courtsAndDoors;
    courtsAndDoors && courtsAndDoors.length > 0 && courtsAndDoors.map(court=>{
      DB.my.isCastOk({proprietorId: proprietorId, courtId: court.id, code: code}).then(
        function (content) {
          t.data.courtsAndDoors = update(t.data.courtsAndDoors, {$apply: courts => {
            return courts.map(c=>{
              if (c.id === court.id) {
                return update(c, {
                  isCastOk: {$set: content}
                });
              } else {
                return c;
              }
            })
          }});
          t.updateComponent();
        },
        function (error) {
        }
      );
    });
  },

  initAuthInfo(proprietorId) {
    let t = this;
    let courtsAndDoors = t.data.courtsAndDoors;
    courtsAndDoors.map(court=>{
      court.doors.map(door=>{
        DB.DoorAuth.authInfo({doorId: door.doorId, pid: proprietorId}).then(
          function (content) {
            t.data.courtsAndDoors = update(t.data.courtsAndDoors, {$apply: cs => {
              return cs.map(c=> {
                if (c.id === court.id) {
                  return update(c, {
                    doors: {$apply: doors => {
                      return doors.map(d=>{
                        if (d.doorId === door.doorId) {
                          return update(d, {
                            authInfo: {$set: content}
                          });
                        } else {
                          return d;
                        }
                      })
                    }}
                  });
                } else {
                  return c;
                }
              });
            }});
            t.updateComponent();
          },
          function (error) {
            console.log(error);
          }
        );
      })
    });
  },

  initDoorState() {

    let t = this;
    let courtsAndDoors = t.data.courtsAndDoors;
    courtsAndDoors.map(court => {
      DB.WechatAPI.findRenWuActive({courtId: court.id}).then(
        function (content) {
          if (content) {
            t.data.courtsAndDoors = update(t.data.courtsAndDoors, {
              $apply: (courts) => {
                return courts.map( c => {
                  if (c.id === court.id) {
                    return update(c, {
                      activeRw: {$set: content.name},
                      rwCode: {$set: content.code},
                      doors: {
                        $apply: (doors) => {
                          return doors.map( d => {
                            let voteState = false;
                            let castState = false;
                            switch(content.code) {
                              case 'CAST01':
                                voteState = d.rightUnit;
                                castState = d.castUnit;
                                break;
                              case 'CAST02':
                                voteState = d.rightBuilding;
                                castState = d.castBuilding;
                                break;
                              case 'CAST03':
                                voteState = d.rightMember;
                                castState = d.castMember;
                                break;
                              case 'CAST04':
                                voteState = d.rightHead;
                                castState = d.castHead;
                                break;
                              default:
                                break;
                            }
                            return update(d, {
                              voteState: {$set: voteState},
                              castState: {$set: castState}
                            })
                          })
                        }
                      }
                    })
                  } else {
                    return c;
                  }
                })
              }
            });
          } else {
            t.data.courtsAndDoors = update(t.data.courtsAndDoors, {
              $apply: (courts) => {
                return courts.map( c => {
                  if (c.id === court.id) {
                    return update(c, {
                      activeRw: {$set: null},
                      rwCode: {$set: null},
                      doors: {
                        $apply: (doors) => {
                          return doors.map( d => {
                            return update(d, {
                              voteState: {$set: false},
                              castState: {$set: false}
                            })
                          })
                        }
                      }
                    })
                  } else {
                    return c;
                  }
                })
              }
            });
          }
          t.updateComponent();
        },
        function (error) {
        }
      );
    })
  },

  getDoorGroupCourt(proprietorId) {
    let t = this;
    DB.WechatAPI.getDoorGroupCourt({proprietorId: proprietorId}).then(
      function (content) {
        t.data.courtsAndDoors = update(t.data.courtsAndDoors, {$set: content});
        t.updateComponent();
        t.initCast(proprietorId, 'CAST01');
        t.initAuthInfo(proprietorId);
        t.initDoorState();
      },
      function (error) {
        console.info(error);
      }
    );
  },

  // 报名
  doSignUp(doorId) {
    let t = this;
    DB.WechatAPI.bindCastok({'doorId': doorId}).then(
      function (content) {
        if (content) {
          Toast.success('报名成功');
          // 重新获取门牌号信息
          t.getDoorGroupCourt(StorageUtils.getStorageItem('proprietor').id);
        } else {
          Toast.success('报名失败');
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  // 取消报名
  doUnSignUp(doorId) {
    let t = this;
    DB.WechatAPI.bindCastno({'doorId': doorId}).then(
      function (content) {
        if (content) {
          Toast.success('取消报名成功');
          // 重新获取门牌号信息
          t.getDoorGroupCourt(StorageUtils.getStorageItem('proprietor').id);
        } else {
          Toast.success('取消报名失败');
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});

